package tw.com.softleader.fire.setting.web;

import java.util.List;

import org.joda.time.LocalDateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import tw.com.softleader.fire.setting.entity.InsuredItemEntity;
import tw.com.softleader.fire.setting.service.InsuredItemService;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 
 * @author Yuanchieh(Ebi)
 * 
 */

public class InsuredItemAction extends ActionSupport {

	private Logger log = LoggerFactory.getLogger(InsuredItemAction.class);

	@Autowired
	private InsuredItemService insuredItemService;

	private List<InsuredItemEntity> models;

	private InsuredItemEntity model;

	public List<InsuredItemEntity> getModels() {
		return models;
	}

	public void setModels(List<InsuredItemEntity> models) {
		this.models = models;
	}

	public InsuredItemEntity getModel() {
		return model;
	}

	public void setModel(InsuredItemEntity model) {
		this.model = model;
	}

	@Override
	public void validate() {

	}
	
	@Override
	public String execute() {
		log.debug("execute...");

		models = insuredItemService.getAll();
		log.debug("Models = {}", models);

		return Action.SUCCESS;
	}
	
	public String select() {
		log.debug("select...");
		
		models = insuredItemService.getByCode(model.getCode());
		log.debug("Models = {}", models);
		
		return Action.SUCCESS;
	}

	public String insert() {
		log.debug("insert...");

		LocalDateTime current = LocalDateTime.now();
		model.setCreator("system");
		model.setCreateTime(current);
		model.setModifier("system");
		model.setModifiedTime(current);

		try {
			insuredItemService.insert(model);
		} catch (Exception e) {
			e.printStackTrace();
		}

		models = insuredItemService.getAll();
		log.debug("Models = {}", models);

		return Action.SUCCESS;
	}

	public String update() {
		log.debug("update...");

		model.setModifiedTime(LocalDateTime.now());

		try {
			insuredItemService.update(model);
		} catch (Exception e) {
			e.printStackTrace();
		}

		models = insuredItemService.getAll();
		log.debug("Models = {}", models);

		return Action.SUCCESS;
	}

	public String delete() {
		log.debug("delete...");

		try {
			insuredItemService.delete(model);
		} catch (Exception e) {
			e.printStackTrace();
		}

		models = insuredItemService.getAll();
		log.debug("Models = {}", models);

		return Action.SUCCESS;
	}
}
